package io.github.codelifeleno.jdbrelease.execute;

import cn.hutool.core.io.resource.Resource;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.core.util.XmlUtil;
import io.github.codelifeleno.jdbrelease.config.DBConfig;
import io.github.codelifeleno.jdbrelease.config.FileConfig;
import io.github.codelifeleno.jdbrelease.config.JDBReleaseConfig;
import org.apache.commons.configuration2.XMLConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.io.URLConnectionOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

import java.nio.file.FileSystem;
import java.util.List;

/**
 * # JDBReleaseConfig.xml 配置文件加载过程
 * # 主程序配置加载过程。
 */
public class JDBReleaseInit {
    private static final Logger log = LoggerFactory.getLogger(JDBReleaseInit.class);

    /**
     * 加载配置的过程
     * 1. 在对应的路径中配置配置文件
     */
    public JDBReleaseConfig initConfig(Resource configFile) {

        JDBReleaseConfig init = null;
        try {
            XMLConfiguration xmlConfig = new FileBasedConfigurationBuilder<>(XMLConfiguration.class)
                    .configure(new Parameters().xml()
                                    .setURL(configFile.getUrl())
                                    .setValidating(false)
                                    .setSchemaValidation(false)
                    )
                    .getConfiguration();
            String driver = xmlConfig.getString("DB.Driver");
            String url = xmlConfig.getString("DB.Url");
            String user = xmlConfig.getString("DB.Username");
            String password = xmlConfig.getString("DB.Password");
            String schema = xmlConfig.getString("DB.Schema");
            String type = xmlConfig.getString("DB.Type");
            DBConfig dbConfig = new DBConfig(driver, url, user, password, schema, type);

            String nameSplit = xmlConfig.getString("FILE.NameSplit");
            String simplePrefix = xmlConfig.getString("FILE.SimplePrefix");
            String rollbackPrefix = xmlConfig.getString("FILE.RollbackPrefix");
            String allwaysPrefix = xmlConfig.getString("FILE.AlwaysPrefix");
            String nameRegExp = xmlConfig.getString("FILE.NameRegExp");
            FileConfig fileConfig = new FileConfig(nameSplit, simplePrefix, rollbackPrefix, allwaysPrefix, nameRegExp);

            String logsDirPath = xmlConfig.getString("LogsDirPath");
            List<String> fileDirPaths = xmlConfig.getList(String.class, "FileDirPaths.FileDirPath");

            init = new JDBReleaseConfig(dbConfig, fileConfig, logsDirPath, fileDirPaths);

            log.debug("JDBReleaseConfig:{}", init);
            log.info("JDBReleaseConfig init success");
        } catch (ConfigurationException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException("配置文件加载失败，请检查配置！");
        }

        return init;
    }
}
